from django.shortcuts import render
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import GenericViewSet, ViewSet, ReadOnlyModelViewSet
# Create your views here.
from qa.serializers import QuestionAllSerializer, AnwserAllSerializer,  AnwserSerializer
from .models import Question, Anwser


# class QuestionListVPIView(APIView):
#     # {
#     #     "id": 1,
#     #     "question: {
#     #         title": "有关PHP初级进阶的问题？",
#     #         "browsing_volume": "50",
#     #         "user_id": "毕鹏",
#     #         "create_time": "2017-07-05 15:09",
#     # },
#     # "anwser"：{
#     #              "user": "luckness",
#     #              "create_time": "3分钟之前",
#     #          },
#     # "label": ["Php", "Javascript"]
#     # }
#     def get(self, request):
#         questions = Question.objects.all()
#         data = []
#         for question in questions:
#             anwser = Anwser.objects.filter(question_id = question.id).order_by("create_time")[0]
#             ques = QuestionAllSerializer(question)
#             anw = AnwserAllSerializer(anwser)
#
#             q_dict = {
#                 "question": ques.data,
#                 "anwser": anw,
#             }
#             data = data.append(q_dict)
#
#         return Response(request.data)

# class QuestionListVPIView(GenericViewSet, ListModelMixin):
#
#     queryset = Anwser.objects.all()
#     serializer_class = AnwserAllSerializer
#
#     # data = []
#
#     def get(self, request):
#         qs = self.get_queryset()
#         serializer = self.get_serializer(qs, many=True)
#
#         return Response(serializer.data)


class QuestionListVPIView(ReadOnlyModelViewSet):
    queryset = Anwser.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AnwserAllSerializer
        else:
            return AnwserSerializer


# class QuestionAPIView(ReadOnlyModelViewSet):
#     query = Anwser.objects.all()
#
#     def get_querset():
#         if self.action == "list":
#             if query:
#                 return query
#             else:
#                 queryset = Question.objects.all()
#                 return queryset
#         else:
#             queryset = Answer.objects.all()
#             if queryset:
#                 return queryset
#             else:
#                 queryset = Question.objects.all()
#                 return queryset
#
#     def get_serializer():
#         if self.action == "list":
#             if query:
#                 return serializers.QuestionAllSerializer
#             else:
#                 return serializers.AnwserAllSerializer
#
#         else:
#             if query:
#                 return serializers.QuestionAllSerializer
#             else:
#                 return serializers.AnwserAllSerializer

